home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / contrib / ras / ras2tif.c < prev    next >
C/C++ Source or Header  |  1991-05-19  |  7KB  |  245 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)ras2tif.c 1.2 90/03/06";
  3. #endif
  4. /*-
  5.  * ras2tif.c - Converts from a Sun Rasterfile to a Tagged Image File.
  6.  *
  7.  * Copyright (c) 1990 by Sun Microsystems, Inc.
  8.  *
  9.  * Author: Patrick J. Naughton
  10.  * naughton@wind.sun.com
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose and without fee is hereby granted,
  14.  * provided that the above copyright notice appear in all copies and that
  15.  * both that copyright notice and this permission notice appear in
  16.  * supporting documentation.
  17.  *
  18.  * This file is provided AS IS with no warranties of any kind.  The author
  19.  * shall have no liability with respect to the infringement of copyrights,
  20.  * trade secrets or any patents by this file or any part thereof.  In no
  21.  * event will the author be liable for any lost revenue or profits or
  22.  * other special, indirect and consequential damages.
  23.  *
  24.  * Comments and additions should be sent to the author:
  25.  *
  26.  *                     Patrick J. Naughton
  27.  *                     Sun Microsystems
  28.  *                     2550 Garcia Ave, MS 14-40
  29.  *                     Mountain View, CA 94043
  30.  *                     (415) 336-1080
  31.  *
  32.  * Revision History:
  33.  * 11-Jan-89: Created.
  34.  * 06-Mar-90: fix bug in SCALE() macro.
  35.  *          got rid of xres and yres, (they weren't working anyways).
  36.  *          fixed bpsl calculation.
  37.  *
  38.  * Description:
  39.  *   This program takes a Sun Rasterfile [see rasterfile(5)] as input and
  40.  * writes a MicroSoft/Aldus "Tagged Image File Format" image or "TIFF" file.
  41.  * The input file may be standard input, but the output TIFF file must be a
  42.  * real file since seek(2) is used.
  43.  */
  44.  
  45. #include <stdio.h>
  46. #include <sys/time.h>
  47. #include <pixrect/pixrect_hs.h>
  48. #include "tiffio.h"
  49.  
  50. typedef int boolean;
  51. #define True (1)
  52. #define False (0)
  53. #define    SCALE(x)    (((x)*((1L<<16)-1))/255)
  54.  
  55. boolean     Verbose = False;
  56. boolean     dummyinput = False;
  57. char       *pname;        /* program name (used for error messages) */
  58.  
  59. void
  60. error(s1, s2)
  61.     char       *s1,
  62.                *s2;
  63. {
  64.     fprintf(stderr, s1, pname, s2);
  65.     exit(1);
  66. }
  67.  
  68. void
  69. usage()
  70. {
  71.     error("usage: %s -[vq] [-|rasterfile] TIFFfile\n", NULL);
  72. }
  73.  
  74.  
  75. main(argc, argv)
  76.     int         argc;
  77.     char       *argv[];
  78. {
  79.     char       *inf = NULL;
  80.     char       *outf = NULL;
  81.     FILE       *fp;
  82.     int         depth,
  83.                 i;
  84.     long        row;
  85.     TIFF       *tif;
  86.     Pixrect    *pix;        /* The Sun Pixrect */
  87.     colormap_t  Colormap;    /* The Pixrect Colormap */
  88.     u_short     red[256],
  89.                 green[256],
  90.                 blue[256];
  91.     struct tm  *ct;
  92.     struct timeval tv;
  93.     long        width,
  94.                 height;
  95.     long        rowsperstrip;
  96.     short       photometric;
  97.     short       samplesperpixel;
  98.     short       bitspersample;
  99.     int         bpsl;
  100.     static char *version = "ras2tif 1.0";
  101.     static char *datetime = "1990:01:01 12:00:00";
  102.  
  103.     gettimeofday(&tv, (struct timezone *) NULL);
  104.     ct = localtime(&tv.tv_sec);
  105.     sprintf(datetime, "19%02d:%02d:%02d %02d:%02d:%02d",
  106.         ct->tm_year, ct->tm_mon + 1, ct->tm_mday,
  107.         ct->tm_hour, ct->tm_min, ct->tm_sec);
  108.  
  109.     setbuf(stderr, NULL);
  110.     pname = argv[0];
  111.  
  112.     while (--argc) {
  113.     if ((++argv)[0][0] == '-') {
  114.         switch (argv[0][1]) {
  115.         case 'v':
  116.         Verbose = True;
  117.         break;
  118.         case 'q':
  119.         usage();
  120.         break;
  121.         case '\0':
  122.         if (inf == NULL)
  123.             dummyinput = True;
  124.         else
  125.             usage();
  126.         break;
  127.         default:
  128.         fprintf(stderr, "%s: illegal option -%c.\n", pname,
  129.             argv[0][1]);
  130.         exit(1);
  131.         }
  132.     } else if (inf == NULL && !dummyinput) {
  133.         inf = argv[0];
  134.     } else if (outf == NULL)
  135.         outf = argv[0];
  136.     else
  137.         usage();
  138.     }
  139.  
  140.     if (outf == NULL)
  141.     error("%s: can't write output file to a stream.\n", NULL);
  142.  
  143.     if (dummyinput || inf == NULL) {
  144.     inf = "Standard Input";
  145.     fp = stdin;
  146.     } else if ((fp = fopen(inf, "r")) == NULL)
  147.     error("%s: %s couldn't be opened.\n", inf);
  148.  
  149.     if (Verbose)
  150.     fprintf(stderr, "Reading rasterfile from %s...", inf);
  151.  
  152.     pix = pr_load(fp, &Colormap);
  153.     if (pix == NULL)
  154.     error("%s: %s is not a raster file.\n", inf);
  155.  
  156.     if (Verbose)
  157.     fprintf(stderr, "done.\n");
  158.  
  159.     if (Verbose)
  160.     fprintf(stderr, "Writing %s...", outf);
  161.  
  162.     tif = TIFFOpen(outf, "w");
  163.  
  164.     if (tif == NULL)
  165.     error("%s: error opening TIFF file %s", outf);
  166.  
  167.     width = pix->pr_width;
  168.     height = pix->pr_height;
  169.     depth = pix->pr_depth;
  170.  
  171.     switch (depth) {
  172.     case 1:
  173.     samplesperpixel = 1;
  174.     bitspersample = 1;
  175.     photometric = PHOTOMETRIC_MINISBLACK;
  176.     break;
  177.     case 8:
  178.     samplesperpixel = 1;
  179.     bitspersample = 8;
  180.     photometric = PHOTOMETRIC_PALETTE;
  181.     break;
  182.     case 24:
  183.     samplesperpixel = 3;
  184.     bitspersample = 8;
  185.     photometric = PHOTOMETRIC_RGB;
  186.     break;
  187.     case 32:
  188.     samplesperpixel = 4;
  189.     bitspersample = 8;
  190.     photometric = PHOTOMETRIC_RGB;
  191.     break;
  192.     default:
  193.     error("%s: bogus depth: %d\n", depth);
  194.     }
  195.  
  196.     bpsl = ((depth * width + 15) >> 3) & ~1;
  197.     rowsperstrip = (8 * 1024) / bpsl;
  198.  
  199.     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
  200.     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
  201.     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
  202.     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  203.     TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
  204.     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
  205.     TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, inf);
  206.     TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "converted Sun rasterfile");
  207.     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  208.     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  209.     TIFFSetField(tif, TIFFTAG_STRIPBYTECOUNTS, height / rowsperstrip);
  210.     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  211.     TIFFSetField(tif, TIFFTAG_SOFTWARE, version);
  212.     TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  213.  
  214.     memset(red, 0, sizeof(red));
  215.     memset(green, 0, sizeof(green));
  216.     memset(blue, 0, sizeof(blue));
  217.     if (depth == 8) {
  218.     TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
  219.     for (i = 0; i < Colormap.length; i++) {
  220.         red[i] = SCALE(Colormap.map[0][i]);
  221.         green[i] = SCALE(Colormap.map[1][i]);
  222.         blue[i] = SCALE(Colormap.map[2][i]);
  223.     }
  224.     }
  225.     if (Verbose)
  226.     fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
  227.  
  228.     for (row = 0; row < height; row++)
  229.     if (TIFFWriteScanline(tif,
  230.                   (u_char *) mprd_addr(mpr_d(pix), 0, row),
  231.                   row, 0) < 0) {
  232.         fprintf("failed a scanline write (%d)\n", row);
  233.         break;
  234.     }
  235.     TIFFFlushData(tif);
  236.     TIFFClose(tif);
  237.  
  238.     if (Verbose)
  239.     fprintf(stderr, "done.\n");
  240.  
  241.     pr_destroy(pix);
  242.  
  243.     exit(0);
  244. }
  245.